package com.xr.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xr.common.annotation.SystemLog;
import com.xr.common.enums.LogType;
import com.xr.system.common.base.MyPage;
import com.xr.system.common.base.Result;
import com.xr.system.common.groups.Save;
import com.xr.system.common.groups.Update;
import com.xr.system.entity.permissionModule.Permission;
import com.xr.system.service.PermissionService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.Size;
import java.io.Serializable;
import java.util.List;

/**
 * 总后台 权限表(Permission)表控制层
 */
@Validated
@RestController
@RequestMapping("/api/v1/permission")
public class PermissionController {

	/**
	 * 服务对象
	 */
	@Resource
	private PermissionService permissionService;

	/**
	 * 获取整个权限树
	 * @return
	 */
	@GetMapping("/permissionTree")
	public Result<List<Permission>> getPermissionTree() {
		return Result.success(permissionService.getPermissionTree());
	}

	/**
	 * 启用/禁用权限
	 * @param id
	 * @return
	 */
	@SystemLog(module = "权限模块", type = LogType.UPDATE, description = "启用/禁用权限")
	@DeleteMapping("/menuOrPermission")
	Result<Object> delMenuOrPermission(Long id) {
		Permission permission = new Permission();
		LambdaUpdateWrapper<Permission> updateWrapper = Wrappers.lambdaUpdate();
		updateWrapper.eq(Permission::getInitial, false).setSql("enable = not enable").eq(Permission::getId, id);
		return Result.isSuccess(permissionService.update(permission, updateWrapper));
	}

	/**
	 * 查询所有数据
	 * @param permission 查询实体
	 * @return 所有数据
	 */
	@GetMapping("/queryAll")
	public Result<List<Permission>> queryAll(Permission permission) {
		permission.setEnable(true);
		return Result.success(permissionService.list(new QueryWrapper<>(permission)));
	}

	/**
	 * 分页查询所有数据
	 * @param page 分页对象
	 * @param permission 查询实体
	 * @return 所有数据
	 */
	@GetMapping("/queryByPage")
	public Result<MyPage<Permission>> queryByPage(MyPage<Permission> page, Permission permission) {
		return Result.success(permissionService.page(page, new QueryWrapper<>(permission)));
	}

	/**
	 * 通过主键查询单条数据
	 * @param id 主键
	 * @return 单条数据
	 */
	@GetMapping("/queryOne/{id}")
	public Result<Permission> selectOne(@PathVariable Serializable id) {
		return Result.success(permissionService.getById(id));
	}

	/**
	 * 新增权限
	 * @param permission 实体对象
	 * @return 新增结果
	 */
	@SystemLog(module = "权限模块", type = LogType.INSERT, description = "新增权限")
	@PostMapping("/insert")
	public Result<Object> insert(@Validated(Save.class) @RequestBody Permission permission) {
		return Result.isSuccess(permissionService.insert(permission));
	}

	/**
	 * 更新权限
	 * @param permission 实体对象
	 * @return 修改结果
	 */
	@SystemLog(module = "权限模块", type = LogType.UPDATE, description = "更新权限")
	@PutMapping("/update")
	public Result<Object> update(@Validated(Update.class) @RequestBody Permission permission) {
		return Result.isSuccess(permissionService.update(permission));
	}

	/**
	 * 删除权限
	 * @param id 主键
	 * @return 删除结果
	 */
	@SystemLog(module = "权限模块", type = LogType.DELETE, description = "删除权限")
	@DeleteMapping("/delete/{id}")
	public Result<Object> delete(@PathVariable Long id) {
		Permission permission = new Permission();
		LambdaUpdateWrapper<Permission> updateWrapper = Wrappers.lambdaUpdate();
		updateWrapper.eq(Permission::getId, id).eq(Permission::getInitial, false);
		return Result.isSuccess(permissionService.deleteWithFill(permission, updateWrapper));
	}

	/**
	 * 批量删除权限
	 * @param idList 主键结合
	 * @return 删除结果
	 */
	@SystemLog(module = "权限模块", type = LogType.DELETE, description = "批量删除权限")
	@DeleteMapping("/batchDelete")
	public Result<Object> batchDelete(
			@Validated @Size(min = 1, message = "缺少删除所需的id") @RequestParam("idList") List<Long> idList) {
		Permission permission = new Permission();
		permission.setInitial(false);
		return Result.isSuccess(permissionService.batchDelete(idList, permission));
	}

}
